﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class admin_MyUserControls_TH_SanPhamAdmin : System.Web.UI.UserControl
{
    private DataSet objSanPhamDS;
    private DataSet objDataSet;
    protected void Page_Load(object sender, EventArgs e)
    {
        // Load the grid only the first time the page is loaded
        if (!Page.IsPostBack)
        {
            BindGrid();
        }
    }

    // Populate the GridView with data
    private void BindGrid()
    {
        // Get CategoryID from the query string
        string loaisp = Request.QueryString["loaisp"];
        string nsx = Request.QueryString["nsx"];

        SanPhamBUS objSanPham = new SanPhamBUS(TGDDConfiguration.DbConnectionString);
        try
        {
            objSanPhamDS = objSanPham.SelectSanPhamByLoaiSP_NSX(int.Parse(loaisp), int.Parse(nsx));
            grid.DataSource = objSanPhamDS.Tables["SelectSanPhamByLoaiSP_NSX"];
            grid.DataBind();
        }
        catch (System.Exception ex)
        {
            throw new Exception(ex.Message, ex.InnerException);
        }
    }

    protected void grid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        // Get the ID of the record to be deleted
        string loaisp = Request.QueryString["loaisp"];
        string id = grid.DataKeys[e.RowIndex].Value.ToString();

        LoaiSanPhamBUS objLoaiSP = new LoaiSanPhamBUS(TGDDConfiguration.DbConnectionString);
        objDataSet = objLoaiSP.SelectTenLoaiSanPham(int.Parse(loaisp));
        DataRow row = objDataSet.Tables[0].Rows[0];
        string tenloaisp = row["TenLoaiSanPham"].ToString();

        SanPhamBUS objSanPham = new SanPhamBUS(TGDDConfiguration.DbConnectionString);
        try
        {
            // Execute the delete command
            bool success = objSanPham.DeleteSanPham(int.Parse(id));
            // Cancel edit mode
            grid.EditIndex = -1;
            // Display status message
            lblStatus.Text = success ? "Xóa thành công" : "Xóa thất bại";
            // Reload the grid
            BindGrid();
        }
        catch (System.Exception ex)
        {
            throw new Exception(ex.Message, ex.InnerException);
        }
    }
    protected void lkbtnThemSanPham_Click(object sender, EventArgs e)
    {
        string loaisp = Request.QueryString["loaisp"];
        string nsx = Request.QueryString["nsx"];
        Response.Redirect("ThemSanPhamMoi.aspx?loaisp="+int.Parse(loaisp)+"&nsx="+int.Parse(nsx));
    }
    protected void grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grid.PageIndex = e.NewPageIndex;
        BindGrid();  
    }
    protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton l = (LinkButton)e.Row.FindControl("lnkbtnDelete");
            l.Attributes.Add("onclick", "javascript:return " +
            "confirm('Bạn có chắc chắn muốn xóa không?' )");
        }
    }
}
